from selenium import webdriver
from selenium.webdriver import ChromeOptions
#导入By类
from selenium.webdriver.common.by import By
#导入显示等待类
from selenium.webdriver.support.ui import WebDriverWait
#导入期望场景类
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions as EC

import time
from lxml import etree

chromeDriverPath='../chromedriver_win32/chromedriver.exe'
accountAddress='127.0.0.1:9222'
url_path='https://cp.kuaishou.com/profile'
bro=None
header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}

if __name__=='__main__':
    video_path='E:/gitProject/pythonProject/2023/05/17/video/youtube/AwakeningDream.mp4'
    #加载cookies中已经保存的账号和密码
    options = ChromeOptions()
    options.add_experimental_option("debuggerAddress", accountAddress)
    bro=webdriver.Chrome(executable_path=chromeDriverPath,chrome_options=options)
    print("开始")
    bro.get(url_path)
    #time.sleep(5)
    wait=WebDriverWait(bro,10,poll_frequency=0.5,ignored_exceptions=None)

    #div_btn=bro.find_element(By.XPATH,'//*[@id="app"]/div[1]/div[1]/div/section/ul/div')
    #div_btn.click()
    div_btn=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="app"]/div[1]/div[1]/div/section/ul/div')))
    div_btn.click()
    #upload_btn=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="onvideo_creator_platform"]/div/section/div[2]/div[1]/div[2]/button')))
    #upload_btn.click()
    input_file=wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="onvideo_creator_platform"]/div/input')))
    print(input_file)
    input_file.send_keys(video_path)
    tmpnum=0
    while True:
        time.sleep(1)
        page_text=bro.page_source
        tree=etree.HTML(page_text)
        upload_status_text=tree.xpath('//*[@id="onvideo_creator_platform"]/div/div[3]/div[2]/div[2]/div/div[2]//text()')
        #upload_status_div=bro.find_element(By.XPATH,'//*[@id="onvideo_creator_platform"]/div/div[3]/div[2]/div[2]/div/div[2]')
        print(upload_status_text)
        if upload_status_text:
            upload_status_text=upload_status_text[0]
        if '上传成功'==upload_status_text:
            break
        if tmpnum>600:
            break
        tmpnum=tmpnum+1
    print('上传完成')
    fabu_div=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="onvideo_creator_platform"]/div/div[3]/div[2]/div[4]/div[2]/div[7]/button[1]')))
    fabu_div.click()
    bro.close
    print("结束")
